package com.weiaibenpao.demo.chislim.hurui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.hurui.adapte.MarathonAdapter;
import com.weiaibenpao.demo.chislim.hurui.bean.MyMarathonBean;
import com.weiaibenpao.demo.chislim.ui.MarathonWebActivity;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_DETAILURL;
import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_ID;
import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_TITLE;
import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_TYPE;

public class MyMarathonActivity extends HBaseActivity {

    private static int PAGE_NUM = 0;
    int num = PAGE_NUM;
    @BindView(R.id.marathon_back)
    ImageView marathonBack;
    @BindView(R.id.recycler_view_marathon)
    PullLoadMoreRecyclerView recyclerViewMarathon;
    private MarathonAdapter marathonAdapter;
    private Context context= new WeakReference<Activity>(this).get();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_marathon);
        ButterKnife.bind(this);
        initAdapter();
        initData(0);
    }

    private void initAdapter() {
        recyclerViewMarathon.setLinearLayout();
        marathonAdapter = new MarathonAdapter(getApplicationContext());
        recyclerViewMarathon.setAdapter(marathonAdapter);
        recyclerViewMarathon.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                num=PAGE_NUM;
                initData(0);
            }

            @Override
            public void onLoadMore() {
                num++;
                initData(1);
            }
        });
        marathonAdapter.setOnItemClickListener(new MarathonAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(MyMarathonBean.DataBean.MarathonlistBean bean) {
//                intent.putExtra(MARATHON_ID,dataBean.getId()+"");
//                intent.putExtra(MARATHON_TITLE,dataBean.getMarathonTitle()+"");
//                intent.putExtra(MARATHON_TYPE,dataBean.getType()+"");
//                intent.putExtra(MARATHON_DETAILURL,dataBean.getDetailUrl());
//                intent.putExtra(MARATHON_TITLE,dataBean.getMarathonTitle());
                Intent intent = new Intent(context, MarathonWebActivity.class);
                intent.putExtra(MARATHON_ID,bean.getId()+"");
                intent.putExtra(MARATHON_TYPE,bean.getType()+"");
                intent.putExtra(MARATHON_DETAILURL,bean.getDetailUrl());
                intent.putExtra(MARATHON_TITLE,bean.getMarathonTitle());

                startActivity(intent);
            }
        });
    }

    private void initData(final int n) {
        apiStores.getMarathon(UserBean.getUserBean().userId,num+" ","20").enqueue(new Callback<MyMarathonBean>() {
            @Override
            public void onResponse(Call<MyMarathonBean> call, Response<MyMarathonBean> response) {
                    if (response.body()!=null){
                        if (response.body().getCode()==0){
                            if(n == 0){   //刷新
                                marathonAdapter.refresh(response.body().getData().getMarathonlist());
                            }else {   //  加载
                                marathonAdapter.loadMore(response.body().getData().getMarathonlist());
                            }
                        }
                    }
            }

            @Override
            public void onFailure(Call<MyMarathonBean> call, Throwable throwable) {

            }
        });
        recyclerViewMarathon.setPullLoadMoreCompleted();
    }

    public static void openActivity(Context context) {
        ((Activity) context).startActivity(new Intent(context, MyMarathonActivity.class));
    }

    @OnClick(R.id.marathon_back)
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.marathon_back:
                finish();
                break;
        }
    }
}
